home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT60.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  8.3 KB  |  277 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0
  4.                  Demonstration Program 60
  5.  
  6.  This program demonstrates use of the sprite library to create a simple      
  7.  BREAKOUT game. The user moves a "paddle" with the mouse and hits the ball   
  8.  around the screen, trying to knock out the bricks.                          
  9.  
  10.  *** PROJECT ***                                                             
  11.  This program requires the WGT5_WC.LIB and WSPR_WC.LIB files to be linked.   
  12.                                           
  13.  *** DATA FILES ***                                                          
  14.  BREAK.SPR                                                                   
  15.                                WATCOM C++ VERSION 
  16. ==============================================================================
  17. */
  18. #include <stdio.h>
  19. #include <dos.h>
  20. #include <conio.h>
  21. #include <stdlib.h>
  22. #include <malloc.h>
  23. #include <wgt5.h>
  24. #include <wgtspr.h>
  25.  
  26. color palette[256];             /* Our palette */
  27. block sprites[31];              /* Sprites to be loaded */
  28. short x, y, i;                    /* Counters and position variables */
  29. short chk1, chk2, chk3, chk4;     /* Result of pixel checks */
  30. short blk[10][28] = {             /* Our wall of bricks is defined here */
  31.    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  32.    0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  33.    0,3,4,3,3,3,4,3,3,3,3,3,4,4,4,4,4,3,3,3,3,3,4,4,4,4,4,3,
  34.    0,3,4,3,3,3,4,3,3,3,3,3,4,3,3,3,3,3,3,3,3,3,3,3,4,3,3,3,
  35.    0,3,4,3,3,3,4,3,3,3,3,3,4,3,3,3,3,3,3,3,3,3,3,3,4,3,3,3,
  36.    0,3,4,3,4,3,4,3,3,3,3,3,4,3,4,4,4,3,3,3,3,3,3,3,4,3,3,3,
  37.    0,3,4,3,4,3,4,3,3,3,3,3,4,3,3,3,4,3,3,3,3,3,3,3,4,3,3,3,
  38.    0,3,4,3,4,3,4,3,3,3,3,3,4,3,3,3,4,3,3,3,3,3,3,3,4,3,3,3,
  39.    0,3,4,4,4,4,4,3,3,3,3,3,4,4,4,4,4,3,3,3,3,3,3,3,4,3,3,3,
  40.    0,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
  41.  
  42.  
  43. void dobounce(void);            /* Makes a sound when the ball bounces */
  44.  
  45. float sbx, sby, sp, lx, ly;     /* Speed of ball and position of ball */
  46. float xsp, ysp;                 /* More motion variables */
  47.  
  48. void looper();                  /* Main program loop is in this routine */
  49. void hit(short, short);             /* Performs update when ball hits brick */
  50.  
  51. short hits;                       /* Number of bricks hit */
  52.  
  53.  
  54. void main (void)
  55. {
  56.   i = minit ();                 /* Initialize mouse handler */
  57.   if (i == 0)
  58.   {
  59.     printf ("Mouse not detected.  You need a mouse for this example program\n");
  60.     printf ("Press any key\n");
  61.     getch ();
  62.     exit (1);
  63.   }
  64.   else printf ("Mouse with %i buttons detected.\n", i);
  65.  
  66.   if (!vgadetected ())
  67.   {
  68.     printf ("VGA is required to run this program...");
  69.     exit (1);
  70.   }
  71.  
  72.   printf ("WGT Example #60\n\n");
  73.   printf ("This program demonstrates use of the sprite library to create a simple\n");
  74.   printf ("BREAKOUT game. The user moves a paddle with the mouse and hits the ball\n");
  75.   printf ("around the screen, trying to knock out the bricks.\n");
  76.   printf ("Clicking the right mouse button will quit.\n");
  77.   printf ("\nPress any key\n");
  78.   getch ();
  79.  
  80.   /* Now start VGA mode, load the sprites and set the palette */
  81.  
  82.   vga256 ();
  83.   wloadsprites (palette, "break.spr", sprites, 0, 30);
  84.   wsetpalette (0, 255, palette);
  85.  
  86.   /* Initialize the sprite system and tell it we've got 2 sprites to use */
  87.   initialize_sprites (sprites);
  88.   maxsprite = 2;
  89.  
  90.   wsetscreen (spritescreen);    /* Draw our game screen on the sprite screen */
  91.  
  92.   for (y = 0; y < 200; y++)     /* Background is a series of gray lines */
  93.   {
  94.     wsetcolor ((y / 8) + 1);
  95.     wline (0, y, 319, y);
  96.   }
  97.   wsetcolor (0);                 /* Make black playing area */
  98.   wbar (50, 10, 270, 189);
  99.  
  100.   wsetcolor (16);                /* Outline it */
  101.   wrectangle (49, 9, 271, 190);
  102.  
  103.   for (x = 1; x < 28; x++)      /* Now draw the bricks */
  104.     for (y = 1; y < 10; y++)
  105.     {
  106.       wputblock (x * 7 + 57, y * 5 + 20, sprites[blk[y][x]], 0);
  107.     }
  108.  
  109.   /* After it has been drawn, show it on the visual screen too */      
  110.   wcopyscreen (0, 0, 319, 199, spritescreen, 0, 0, NULL);
  111.   wcopyscreen (0, 0, 319, 199, spritescreen, 0, 0, backgroundscreen);
  112.   wnormscreen ();
  113.  
  114.   spriteon (0, 160, 100, 1);    /* Turn on our sprites */
  115.   spriteon (1, 160, 100, 2);
  116.  
  117.   msetbounds (50, 129, 245, 179);       /* Limit mouse movement */
  118.  
  119.   xsp = 1.1;                    /* Set initial ball speeds */
  120.   ysp = 1.3;
  121.  
  122.   sbx = xsp;
  123.   sby = ysp;
  124.   lx = 160;                     /* And the initial position */
  125.   ly = 100;
  126.  
  127.   msetxy (160, 150);
  128.   do {                          /* Now play the game */
  129.     looper ();
  130.   } while (mouse.but != 2);     /* Until the right mouse button is clicked */
  131.  
  132.   deinitialize_sprites ();      /* Deinit the sprite system */
  133.   mdeinit ();                   /* And the mouse handler */
  134.   wsetmode (3);                 /* Return to text mode */
  135. }
  136.  
  137.  
  138. void looper(void)
  139. {
  140.   wretrace ();                  /* Time our updates to the vertical retrace */
  141.   erase_sprites ();             /* Clear sprites from sprite screen */
  142.  
  143.   s[0].x = mouse.mx;                  /* Set paddle position to mouse position */
  144.   s[0].y = mouse.my;
  145.  
  146.   if (lx > 267)                 /* Is ball bouncing off right wall? */
  147.   {
  148.     dobounce ();
  149.     lx = 267;
  150.     sbx = -sbx;                 /* Change direction */
  151.   }
  152.   if (lx < 49)                  /* Is ball bouncing off left wall? */
  153.   {
  154.     dobounce ();
  155.     lx = 49;
  156.     sbx = -sbx;                 /* Change direction */
  157.   }
  158.   if (ly < 9)                   /* Is ball bouncing off top wall? */
  159.   {
  160.     dobounce ();
  161.     ly = 9;
  162.     sby = -sby;                 /* Change direction */
  163.   }
  164.  
  165.   lx += sbx;                    /* Update  ball positions */
  166.   ly += sby;
  167.   s[1].x = (float)lx;
  168.   s[1].y = (float)ly;
  169.  
  170.   if (s[1].y > 186)             /* Did it get by the user and go off the bottom? */
  171.   {
  172.     for (i = 2000; i >= 200; i--)
  173.       sound (i);
  174.     nosound ();
  175.     lx = 160;                   /* Reset it to the starting position */
  176.     ly = 100;
  177.   }
  178.   
  179.   if (overlap (0, 1))           /* Hit the ball with paddle? */
  180.   {
  181.     sound (900);
  182.     sby = -ysp;                 /* Change speed and direction based on
  183.                    part of paddle hit */
  184.     if (s[1].x > s[0].x + 21)
  185.       sbx = xsp * 4;
  186.     else if (s[1].x > s[0].x + 18)
  187.       sbx = xsp * 2;
  188.     else if (s[1].x > s[0].x + 12)
  189.       sbx = xsp;
  190.     else if (s[1].x > s[0].x + 6)
  191.       sbx = -xsp;
  192.     else if (s[1].x > s[0].x + 3)
  193.       sbx = -xsp * 2;
  194.     else
  195.       sbx = -xsp * 4;
  196.   }
  197.  
  198.   /* Now check edges of ball for brick contact */
  199.   chk1 = wgetpixel (s[1].x + 3, s[1].y - 1);    /* Upper edge */
  200.   chk2 = wgetpixel (s[1].x + 3, s[1].y + 6);    /* Lower edge */
  201.   chk3 = wgetpixel (s[1].x - 1, s[1].y + 3);    /* Left edge */
  202.   chk4 = wgetpixel (s[1].x + 6, s[1].y + 3);    /* Right edge */
  203.   if (chk1 > 28)
  204.   {
  205.     hit (3, -1);
  206.     sby = ysp;
  207.   }
  208.   else if (chk2 > 28)
  209.   {
  210.     hit (3, 6);
  211.     sby = -ysp;
  212.   }
  213.   if (chk3 > 28)
  214.   {
  215.     hit (-1, 3);
  216.     sbx = -sbx;
  217.     lx += 2;
  218.   }
  219.   else if (chk4 > 28)
  220.   {
  221.     hit (6, 3);
  222.     sbx = -sbx;
  223.     lx -= 2;
  224.   }
  225.   nosound ();                   /* Turn off all sound */
  226.   draw_sprites (1);             /* Draw the sprites again */
  227. }
  228.  
  229.  
  230.  
  231. void hit (short ix, short iy)
  232. {
  233.   short x1, x2;
  234.   short y1, y2;
  235.  
  236.   sound (600);                  /* Beep because we hit something */
  237.   wsetcolor (0);
  238.   /* Loop through all the bricks to see what we hit */
  239.   for (x = 1; x < 28; x++)
  240.   {
  241.     x1 = x * 7 + 57;
  242.     x2 = x1 + 6;
  243.     for (y = 1; y < 10; y++)
  244.     {
  245.       y1 = y * 5 + 20;
  246.       y2 = y1 + 4;
  247.       if ((s[1].x + ix >= x1) && (s[1].x + ix <= x2)
  248.        && (s[1].y + iy >= y1) && (s[1].y + iy <= y2) && (blk[y][x] != 0))
  249.       {
  250.     wsetscreen (spritescreen);
  251.     wbar (x1, y1, x2, y2);          /* Erase brick */
  252.     wsetscreen (backgroundscreen);
  253.     wbar (x1, y1, x2, y2);          /* Erase brick */
  254.     wnormscreen ();
  255.                     /* And update visual screen */
  256.     wcopyscreen (x1, y1, x2, y2, spritescreen, x1, y1, NULL);
  257.     blk[y][x] = 0;                  /* Disable brick in array */
  258.     hits++;
  259.     if ((hits % 15) == 0)           /* Increase speed after every 15 hits */
  260.     {
  261.       xsp += .1;
  262.       ysp += .1;
  263.     }
  264.       }
  265.     }
  266.   }
  267. }
  268.  
  269.  
  270.  
  271. void dobounce (void)
  272. {
  273.   sound (200);                  /* Beep when bouncing */
  274.   nosound ();
  275. }
  276.  
  277.